import { addToCart, cartList, checkCart, deleteGoodsItem }  from '../../utils/api'
import showToast from '../../utils/tips';
// pages/cart/cart.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        cartList:[],
        isAllChecked:false,
    },
    onChange(e){
        // console.log(e);
        let isChecked = e.detail?1:0;
        let goodsId = e.currentTarget.dataset.goodsid;
        this.getCartCheck(goodsId,isChecked)
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getCartList()
    },
    // 获取购物车列表
    async getCartList(){
        try {
            let result = await cartList();
            if(result.code === 200){
                this.setData({
                    cartList:result.data,
                })
                this.getAllChecked();
            }
        } catch (error) {
            console.log(error);
            showToast('修改失败')
        }
    },
    // 选中与取消选中购物车商品
    async getCartCheck(goodsId, isChecked){
        try {
            let result = await checkCart(goodsId, isChecked);
            result.code === 200 && this.getCartList()
        } catch (error) {
            console.log(error);
            showToast('修改状态失败');
        }
    },
    // 删除商品的回调
    deleteGoods(e){
        let {goodsid,name} = e.currentTarget.dataset;
        wx.showModal({
            title: '确认删除',
            content: name,
            showCancel: true,
            cancelText: '取消',
            cancelColor: '#000000',
            confirmText: '确定',
            confirmColor: '#3CC51F',
            success: (result) => {
              // 用户点击确定按钮
              if (result.confirm) {
                this.deleteDel(goodsid)
              }
            }
          });
    },
    // 删除商品
    async deleteDel(goodsId){
        try {
            let result = await deleteGoodsItem(goodsId);
            result.code === 200 && this.getCartList();
        } catch (error) {
            console.log(error);
            showToast('删除失败');
        }
    },
    // 点击购物车商品修改商品
    changeCount(e){
        // console.log(e.detail);
        let newCount = e.detail*1;
        let {goodsid,oldcount} = e.currentTarget.dataset;
        let count =newCount - oldcount ;
       count !==0 &&  this.getAddCart({count,goodsid});
    },
    // 选中框按钮功能的实现
    async getAddCart({count,goodsid}){
        try {
            let result = await addToCart({count,goodsId:goodsid});
            console.log(result);
            result.code === 200 && this.getCartList();
        } catch (error) {
            console.log(error);
            showToast('按钮选矿失败');
        }
    },
    // 全选全不选的状态
    getAllChecked(){
        let isAllChecked = this.data.cartList.every(item=>item.isChecked) && this.data.cartList.length > 0;
        this.setData({
            isAllChecked,
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})